using System;
using System.Collections;
using System.Collections.Generic;
using Sirenix.OdinInspector;
using UnityEngine;

[Serializable]
public class HpComponent : BaseComponent
{
    [Header("生命值设置")]
    public float maxHealth = 100f;     // 最大生命值
    public float currentHealth;        // 当前生命值

    public Action<float, float> OnTakeDamage; //受到攻击事件，可以在这个事件中做一些特效处理
    public Action OnDie; //死亡事件

    [Header("血条设置，如果不需要血条则无视")]
    public bool IsHpImage; //是否有血条
    public GameObject HpFollowPoint = null; //血条跟随点
    public bool IsGreen;
    public bool IsPersistence; //是否是持久化血条
    public Vector3 HpImageOffset;
    public bool IsDead = false;
    private AttackEvent attackEvent = new AttackEvent();
    [Title("是否自动回血")]
    public bool IsAddHp; //是否自动回血
    public float AddHpTimeDown;
    public float AddHpCoolden;
    public float AddHpValue; // 一秒恢复多少生命值
    [ReadOnly]
    public bool IsCurrentAddHp; //当前是否正在回血

    public override void OnAwake()
    {
        base.OnAwake();
        Restart();
    }

    public override void OnUpdate()
    {
        base.OnUpdate();
        if (IsAddHp && !IsDead) //死亡后不在回血
        {
            AddHpTimeDown += Time.deltaTime;
            IsCurrentAddHp = false;
            if (AddHpTimeDown >= AddHpCoolden)
            {
                if (currentHealth < 100)
                {
                    currentHealth += AddHpValue * Time.deltaTime;
                    IsCurrentAddHp = true;
                    SendMessageHpChange(currentHealth);
                    if (currentHealth >= 100)
                    {
                        currentHealth = 100;
                    }
                }
            }
        }
        else
        {
            IsCurrentAddHp = false;
        }
    }

    //重启组件
    public void Restart()
    {
        currentHealth = maxHealth;
        IsDead = false;
    }

    // 受到伤害的方法
    public void TakeDamage(float damage, GameObject attackObject)
    {
        AddHpTimeDown = 0;
        // foreach (var e in HitSystems)
        // {
        //     e.Play();
        // }
        currentHealth = Mathf.Max(0, currentHealth - damage);

        OnTakeDamage?.Invoke(maxHealth, currentHealth);

        bool HasTakeDamageEffect = entity.HasEcsComponent<TakeDamageEffectBaseComponent>();
        if (HasTakeDamageEffect)
        {
            TakeDamageEffectBaseComponent[] result = entity.GetEcsComponents<TakeDamageEffectBaseComponent>();
            for (int i = 0; i < result.Length; i++)
            {
                result[i].TakeDamageEffectPlay(attackObject);
            }
        }

        float targetHp = currentHealth + damage;
        SendMessageHpChange(targetHp);
    
        if (currentHealth <= 0)
        {
            Die();
        }

    }

    /// <summary>
    /// 死亡逻辑
    /// </summary>
    public void Die()
    {
        OnDie?.Invoke();
        IsDead = true;
    }

    private void SendMessageHpChange(float TargetHealth)
    {
        if (IsHpImage)
        {
            if (HpFollowPoint == null)
            {
                attackEvent.Target = this.entity.gameObject;
            }
            else
            {
                attackEvent.Target = HpFollowPoint;
            }
            if (IsGreen)
            {
                attackEvent.type = 0;
            }
            else
            {
                attackEvent.type = 1; //红色血条
            }
            attackEvent.BeforePercent = TargetHealth / maxHealth;
            attackEvent.AfterPercent = currentHealth / maxHealth;
            attackEvent.Offset = HpImageOffset;
            attackEvent.IsPersistence = IsPersistence;
            EventManager.Instance.Fire(this, attackEvent);
        }
    }
}
